/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include <hal_rng.h>
/* includes (standard library, system) ---------------------------------------*/
#include <string.h>
#include <stdlib.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_compiler.h>
#include <kal.h>

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

__weak int hal_rng_number(void)
{
    srand((unsigned int)k_mseconds());
    return rand();
}

int hal_rng_buffer(void *buf, size_t len)
{
    size_t i;
    int rn;
    char *pbuf = (char *)buf;

    if (buf == NULL) {
        return -1;
    }

    for (i = 0; i < len; i += sizeof(int)) {
        rn = hal_rng_number();
        memcpy(pbuf + i, &rn, min(sizeof(int), len - i));
    }

    return 0;
}
